<?php
class ModelShopsSelect extends Model {

	//-------------------------------------------------------------------------//
	// Get shop name base on customer_id
	//-------------------------------------------------------------------------//
	public function getShopNameByCustomerId($customer_id) {
		$query = $this->db->query("SELECT shop_id,shop_name FROM " . DB_PREFIX . "shops WHERE customer_id = '" . (int)$customer_id . "'");
		return $query->rows;
	}

	//-------------------------------------------------------------------------//
	// Get shop name base on customer_id
	//-------------------------------------------------------------------------//
	public function getShopInfoByShopName($shopName) {
		$query = $this->db->query("SELECT shop_id FROM " . DB_PREFIX . "shops WHERE shop_name = '" . $shopName . "'");
		return $query->rows;
	}

	//-------------------------------------------------------------------------//
	// Get shop name base on customer_id
	//-------------------------------------------------------------------------//
	public function getShopNameByShopId($shopId) {
		$query = $this->db->query("SELECT shop_name FROM " . DB_PREFIX . "shops WHERE shop_id = '" . $shopId . "'");
		return $query->rows;
	}


	//-------------------------------------------------------------------------//
	// Get shop id base on customer_id
	//-------------------------------------------------------------------------//
	public function getShopIdByCustomerId($customer_id) {
		if(empty($customer_id) == true){
			return null;
		}
		$SQL  = " SELECT shop_id, shop_name FROM " . DB_PREFIX . " shops ";
		$SQL .= " WHERE customer_id = '" . (int)$customer_id . "'";
		$SQL .= " AND status = 1 ";
		$query = $this->db->query($SQL);
		return $query->rows;
	}
	//-------------------------------------------------------------------------//
	// Get shop_id base on customer_id and shop_name
	//-------------------------------------------------------------------------//
	public function getShopId($customer_id, $shop_name){
		$SQL  = " SELECT shop_id FROM " . DB_PREFIX . " shops ";
		$SQL .= " WHERE customer_id = '" . (int)$customer_id . "'";
		$SQL .= " AND shop_name = '" . $shop_name . "'";
		$SQL .= " AND status = 1 ";
		$query   =  $this->db->query($SQL);
		return $query->rows;
	}

	//-------------------------------------------------------------------------//
	// Create new shop
	//-------------------------------------------------------------------------//
	public function createShop($data){
		$SQl_shop = "INSERT INTO " . DB_PREFIX . "shops SET ";
		$SQl_shop .=	"customer_id = '" . (int)$data['customer_id'] . "', ";
		$SQl_shop .=	"shop_name = '" . $data['shop_name'] . "', ";
		$SQl_shop .=	"address = '" . $this->db->escape($data['address']) . "', ";
		$SQl_shop .=	"end_date = now(),";
		$SQl_shop .=	"start_date = now(),";
		$SQl_shop .=	"status = 1, ";
		$SQl_shop .=	"create_date = now()";
		$result    =    $this->db->query($SQl_shop);
		return $result;
	}

	//-------------------------------------------------------------------------//
	// Check duplicate for shop_id and catalog_id
	//-------------------------------------------------------------------------//
	public function checkDuplicate($shop_id, $catalog_id){
		$SQL  = " SELECT count(1) as count FROM " . DB_PREFIX . " shop_to_category ";
		$SQL .= " WHERE shop_id = '" . (int)$shop_id . "'";
		$SQL .= " AND category_id = '" . $catalog_id . "'";
		$query   =  $this->db->query($SQL);
		return $query->row['count'];

	}
}
?>